package com.shoujifeng.snowmusic.player.http;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import com.shoujifeng.snowmusic.player.interfaces.DownloadInterface;

import android.R.integer;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


/**
 * 下载类
 * BY Kress
 * @author Administrator
 *
 */
public class DownLoadFile {
	public String urlString;
	public String savePath;
	public String fileName;
	public String clenght;
	public int index;
	public int uid;
	public int pro;
	
	public DownloadInterface mInterface;
	
	public boolean bStart = true;

	public ProgressDialog DownloadDialog = null;

	// public

	public DownLoadFile() {
	}
	
	public void setDownloadData(int idx, String url, String path, String name) {
		urlString = url;
		savePath = path;
		fileName = name;
		uid = idx;
	}
	
	public void setInterface(DownloadInterface dInterface) {
		mInterface = dInterface;
	}
	
	public void startDownload(int lenght) {
		DownFile(urlString, savePath, fileName, lenght);
	}

	/**
	 * �����ļ�
	 * 
	 * @param context
	 * @param urlString
	 * @return
	 */

	public Boolean DownFile(final String urlString,
			final String savePath, final String fileName, final int slenght) {

		new AsyncTask<Integer, Integer, Integer>() {

			@Override
			protected Integer doInBackground(Integer... params) {
				try {
					
					URL url;
					HttpURLConnection connection = null;
					InputStream inputStream = null;
					OutputStream outputStream = null;
					Integer FileLength;

				
					try {
						url = new URL(urlString);
						connection = (HttpURLConnection)url.openConnection();
						if (connection.getReadTimeout() == 5) {
							
						}
						
						
						if(clenght == null)
							clenght = "0";
						String bytes = "bytes=";
						bytes = bytes + Integer.toString(slenght);
						bytes = bytes + "-" + clenght;
						connection.setRequestMethod ( "POST" ); 
						connection.setRequestProperty("User-Agent","NetFox");
						
						if(slenght > 0)
							connection.setRequestProperty("RANGE",bytes);
						
						inputStream = connection.getInputStream();

					} catch (MalformedURLException e1) {
						e1.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					String savePathString = savePath + "/" + fileName;

					File file = new File(savePathString);
					if (!file.getParentFile().exists())
					{
						file.getParentFile().mkdirs();
					}

					try {
						outputStream = new FileOutputStream(file, true);
						byte[] buffer = new byte[1024 * 4];
						FileLength = connection.getContentLength();
						
						clenght = FileLength.toString();
						
						Integer count = 0;
						Integer havedownlength = slenght;
						
						while ((count = inputStream.read(buffer)) > 0) {
							outputStream.write(buffer, 0, count);
							havedownlength = havedownlength + count;
							
							DownLoadFile.this.pro = havedownlength;
							if(mInterface != null)
								mInterface.downloadding(index, havedownlength, FileLength, !bStart);
							
							//停止
							if(!bStart) {
								break;
							}
						}
						outputStream.flush();
						outputStream.close();
						outputStream = null;

						//如果是中途停止则不写入数据库
						if(bStart) {
							mInterface.downloadOver(index, FileLength, savePathString);
							bStart = false;
						}
						
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						Log.e("error", e.getMessage() + " 1");
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						Log.e("error", e.getMessage() + " 2");
						e.printStackTrace();
					}
				} catch (Exception e) {
					Log.e("error", e.getMessage() + " 2");
					e.printStackTrace();
				}
				return null;
			}
			
		}.execute(0);
		
	
		return true;
	}

}
